package com.ziytek.web.citizen.configuration;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import org.apache.ibatis.session.Configuration;
import org.springframework.cloud.context.config.annotation.RefreshScope;

import java.util.List;

/**
 * @author fenghx
 * @version 1.0
 * @className EasySqlInjector
 * Description: 自定义Sql注入
 * @date 2024/3/25 16:00
 */
@RefreshScope
public class EasySqlInjector extends DefaultSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList(Configuration configuration, Class<?> mapperClass, TableInfo tableInf) {
        // 注意：此SQL注入器继承了DefaultSqlInjector(默认注入器)，调用了DefaultSqlInjector的getMethodList方法，保留了mybatis-plus的自带方法
        var dbConfig = GlobalConfigUtils.getDbConfig(configuration);
        // 生成INSERT语句时忽略自增主键字段(默认不忽略,主键有值时写入主键值,无值自增).
        dbConfig.setInsertIgnoreAutoIncrementColumn(true);
        var methodList = super.getMethodList(configuration, mapperClass, tableInf);
        methodList.add(new InsertBatchSomeColumn());
        // 添加批量更新方法
        methodList.add(new UpdateBatchById(tableInf.getTableName()));
        return methodList;
    }
}

